package com.magus.project.yddemo.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.magus.framework.core.annotation.CustomPagingParam;
import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.core.vobean.PagingParam;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.project.yddemo.entity.IndicatorMapperConfig;
import com.magus.project.yddemo.service.IndicatorMapperConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 指标管理
 * 
 * @author xujuan
 * @date 2018-01-19 15:26:53
 */
@Api(tags = "IndicatorDataExtractionManager", description = "指标数据抽取相关管理")
@RestController
@RequestMapping("/yddemo/indicatorDataExtractions")
@AppGroupInfo(key = "yddemo:indicatorDataExtraction", name = "指标数据抽取相关管理", appKey = "yddemo")
public class IndicatorMapperConfigController extends BaseController {

	@Autowired
	private IndicatorMapperConfigService configService;

	/**
	 * 根据ID查询指标信息
	 * 
	 * @param indicatorId
	 *            指标ID
	 * @return 指标信息
	 */
	@GetMapping
	@RequiresPermissions("yddemo:indicatorDataExtraction:manage")
	@ApiOperation(value = "查询指标", notes = "查询指标信息")
	ResponseEntity<Page<IndicatorMapperConfig>> findAll(@RequestParam(required = false) String companyId,@RequestParam(required = false) String unitId,@CustomPagingParam PagingParam pagingParam) {
		List<SearchFilter> searchFilters = new ArrayList<>();
		if(!companyId.equals("")) {
			searchFilters.add(SearchFilter.equal("company.id", companyId));
		}
		if(!unitId.equals("")) {
			searchFilters.add(SearchFilter.equal("comUnit.id", unitId));
		}
		PageRequest pageRequest = buildPageRequest(pagingParam);
		Specification<IndicatorMapperConfig> spec = buildFilterRequest(searchFilters);
		List<IndicatorMapperConfig> findAll = configService.findAll(spec);
		long total = 0;
		if (pageRequest != null) {
			total = findAll.size();
		}
		Page<IndicatorMapperConfig> page = new PageImpl<IndicatorMapperConfig>(findAll, pageRequest, total);
		return ResponseEntity.ok(page);
	}
	
	@GetMapping("/{configId}")
	@RequiresPermissions("yddemo:indicatorDataExtraction:manage")
	@ApiOperation(value = "查询指标", notes = "根据ID查询指标信息")
	ResponseEntity<IndicatorMapperConfig> findOne(@PathVariable String configId) {
		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("id", configId));
		Specification<IndicatorMapperConfig> spec = buildFilterRequest(searchFilters);
		IndicatorMapperConfig findOne = configService.findOne(spec);
		return ResponseEntity.ok(findOne);
	}

	/**
	 * 新增IndicatorMapperConfig
	 * @param indicatorMapperConfig
	 * @param result
	 * @return
	 */
	@PostMapping
	@RequiresPermissions("yddemo:indicatorDataExtraction:add")
	@ApiOperation(value = "新增指标", notes = "返回成功保存后的对象")
	ResponseEntity<IndicatorMapperConfig> add(@RequestBody IndicatorMapperConfig indicatorMapperConfig, BindingResult result) {
		IndicatorMapperConfig save = configService.save(indicatorMapperConfig);
		return ResponseEntity.status(HttpStatus.CREATED).body(save);
	}

	/**
	 * 更新IndicatorMapperConfig
	 * 
	 * @param indicatorId
	 *            指标ID
	 * @param indicator
	 *            修改的指标数据
	 * @param result
	 *            Spring Framework自已管理的参数
	 * @return 更新后的指标信息
	 */
	@PutMapping("/{configId}")
	@RequiresPermissions("yddemo:indicatorDataExtraction:edit")
	@ApiOperation(value = "编辑指标", notes = "返回成功保存后的对象")
	ResponseEntity<IndicatorMapperConfig> update(@PathVariable String configId, @RequestBody IndicatorMapperConfig indicatorMapperConfig,
			BindingResult result) {
		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("id", configId));
		Specification<IndicatorMapperConfig> spec = buildFilterRequest(searchFilters);
		IndicatorMapperConfig imConfig = configService.findOne(spec);
		if (imConfig == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
		}
		indicatorMapperConfig.setId(imConfig.getId());
		IndicatorMapperConfig save = configService.save(indicatorMapperConfig);
		return ResponseEntity.ok(save);
	}

	/**
	 * 删除指标
	 * 
	 * @param ids
	 *            指标ID
	 * @return 无返回内容
	 */
	@DeleteMapping
	@RequiresPermissions("yddemo:indicatorDataExtraction:delete")
	@ApiOperation(value = "删除指标", notes = "根据ID删除指标")
	@AppFunctionInfo(key = "yddemo:indicatorDataExtraction:delete", name = "删除指标", groupKey = "yddemo:indicatorDataExtraction", //
			isMenu = false, type = FunctionType.BUTTON, url = "/yddemo/indicatorDataExtractions")
	ResponseEntity<Void> delete(@RequestBody List<String> ids) {
		List<SearchFilter> searchFilters = null;
		for (String id : ids) {
			searchFilters = new ArrayList<>();
			searchFilters.add(SearchFilter.equal("id", id));
			Specification<IndicatorMapperConfig> spec = buildFilterRequest(searchFilters);
			IndicatorMapperConfig imConfig = configService.findOne(spec);
			if (imConfig == null) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			configService.delete(imConfig);
		}
		return ResponseEntity.noContent().build();
	}

}
